/*
 * Created by JFormDesigner on Sun Mar 27 12:51:34 CST 2022
 */

package com.wangcanxuan.www.view.tribe;

import com.wangcanxuan.www.controller.Impl.TribeControllerImpl;
import com.wangcanxuan.www.controller.Impl.UserControllerImpl;
import com.wangcanxuan.www.controller.TribeController;
import com.wangcanxuan.www.controller.UserController;
import com.wangcanxuan.www.entity.dto.ResultInfo;
import com.wangcanxuan.www.entity.po.User;
import com.wangcanxuan.www.proxy.ControllerProxy;
import com.wangcanxuan.www.util.ValidateUtil;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;

import static com.wangcanxuan.www.entity.dto.ResultConstants.*;

/**
 * 创建部落界面
 *
 * @author unknown
 * @date 2022/04/01
 */
public class CreateTribeView extends JFrame {
    private final User user;

    public CreateTribeView(User user) {
        this.user = user;
        initComponents();
        setVisible(true);
    }

    private final TribeController tribeController = ControllerProxy.getProxyInstance(TribeControllerImpl.class);
    private final UserController userController = ControllerProxy.getProxyInstance(UserControllerImpl.class);

    private void back(ActionEvent e) {
        // TODO add your code here
        new TribalHallView(user);
        dispose();
    }

    private void createTribe(ActionEvent e) {
        // TODO add your code here
        String tribeName = tribeNameText.getText();
        if (ValidateUtil.strIsNullOrEmpty(tribeName)) {
            JOptionPane.showMessageDialog(null, "部落名不能为空！");
            return;
        }
        ResultInfo<?> tribe = tribeController.createTribe(user, tribeName);
        int code = tribe.getCode();
        String message = tribe.getMessage();
        if (code == SUCCESS_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
            new TribalHallView(userController.refresh(user.getId()).getObj());
            dispose();
        }
        if (code == REJECT_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        label1 = new JLabel();
        label2 = new JLabel();
        backButton = new JButton();
        tribeNameText = new JTextField();
        label3 = new JLabel();
        coinsText = new JTextField();
        label4 = new JLabel();
        createTribeButton = new JButton();

        //======== this ========
        setTitle("\u90e8\u843d\u521b\u5efa");
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u90e8\u843d\u521b\u5efa");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));

        //---- label2 ----
        label2.setText("\u90e8\u843d\u540d \uff1a");

        //---- backButton ----
        backButton.setText("\u8fd4\u56de\u5927\u5385");
        backButton.addActionListener(e -> back(e));

        //---- label3 ----
        label3.setText("\u5f53\u524d\u91d1\u5e01\uff1a");

        //---- coinsText ----
        coinsText.setEditable(false);

        //---- label4 ----
        label4.setText("\u521b\u5efa\u90e8\u843d\u6240\u9700\u91d1\u5e01\u4e3a\uff1a8848");

        //---- createTribeButton ----
        createTribeButton.setText("\u521b\u5efa\u90e8\u843d");
        createTribeButton.addActionListener(e -> createTribe(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGap(49, 49, 49)
                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(label4, GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                                                                        .addComponent(label3, GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                                                                        .addComponent(label2, GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE))
                                                                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                                                        .addComponent(tribeNameText, GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
                                                                        .addComponent(coinsText, GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)))
                                                        .addComponent(createTribeButton, GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)))
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addContainerGap()
                                                .addComponent(backButton, GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(label1, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
                                                .addGap(37, 37, 37)))
                                .addContainerGap(80, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(backButton))
                                .addGap(37, 37, 37)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(tribeNameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label2, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(coinsText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label3, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addComponent(label4, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(createTribeButton)
                                .addContainerGap(67, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
        coinsText.setText(String.valueOf(user.getCoins()));
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JLabel label1;
    private JLabel label2;
    private JButton backButton;
    private JTextField tribeNameText;
    private JLabel label3;
    private JTextField coinsText;
    private JLabel label4;
    private JButton createTribeButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
